package br.ufrn.dca.interfaceGrafica;

import br.ufrn.dca.Comunicacao.Cliente;
import br.ufrn.dca.Comunicacao.TipoPlanta;
import br.ufrn.dca.ExportarDados.Exportador;
import br.ufrn.dca.controle.QuanserClientException;
import br.ufrn.dca.graficos.*;
import br.ufrn.dca.observador.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.Timer;

public class Janela extends javax.swing.JFrame {

    private Exportador exp = new Exportador();
    private JanelaConfigurações jc = new JanelaConfigurações(this, true, this);
    private boolean conectado;
    private boolean configurado;
    private Observador obs;
    private Cliente cliente;
    private Grafico gr = new Grafico("Grafico");
    private Timer contador;
    private double tempoInicial;
    private double tempoAtual = 0.0;
    private double tempoFinal = 0.0;
    //  List <Boolean> graficos = new ArrayList<Boolean>();
    private DecimalFormat aproximador = new DecimalFormat("0.00");
    private TipoPlanta T_Planta;
    private TipoObservador T_Obs;
    private int periodoAmst = 100; //tempo de amostragem em milisegundos

    public Janela() {
        initComponents();

        this.setLocationRelativeTo(null);
        gr.pack();
        this.graficoPanel.add(gr.getContentPane());
        this.configurado = false;

    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        graficoPanel = new javax.swing.JPanel();
        jPanelTanques = new javax.swing.JPanel();
        jPanelTanque2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabelNivelReal_2 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabelNivelEst_2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabelErro_2 = new javax.swing.JLabel();
        jPanelTanque1 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabelNivelReal_1 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabelNivelEst_1 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabelErro_1 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenuCONEXÃO = new javax.swing.JMenu();
        jMenuCONFIGURAÇÔES = new javax.swing.JMenuItem();
        Conectar = new javax.swing.JMenuItem();
        ExecutarObs = new javax.swing.JMenuItem();
        Desconectar = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        graficoPanel.setLayout(new java.awt.BorderLayout());

        jPanelTanques.setFont(new java.awt.Font("Arial Black", 0, 12));

        jPanelTanque2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Tanque 2", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N
        jPanelTanque2.setFont(new java.awt.Font("Tahoma", 1, 12));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel1.setText("Nível Real:");

        jLabelNivelReal_2.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabelNivelReal_2.setText("0.00 cm");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel2.setText("Nível Estimado: ");

        jLabelNivelEst_2.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabelNivelEst_2.setText("0.00 cm");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel3.setText("Erro: ");

        jLabelErro_2.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabelErro_2.setText("0.00 cm ");

        javax.swing.GroupLayout jPanelTanque2Layout = new javax.swing.GroupLayout(jPanelTanque2);
        jPanelTanque2.setLayout(jPanelTanque2Layout);
        jPanelTanque2Layout.setHorizontalGroup(
            jPanelTanque2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelTanque2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelTanque2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanelTanque2Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabelNivelReal_2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanelTanque2Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabelNivelEst_2, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabelErro_2, javax.swing.GroupLayout.DEFAULT_SIZE, 79, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanelTanque2Layout.setVerticalGroup(
            jPanelTanque2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelTanque2Layout.createSequentialGroup()
                .addGroup(jPanelTanque2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelTanque2Layout.createSequentialGroup()
                        .addGroup(jPanelTanque2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jLabelNivelReal_2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
                        .addGroup(jPanelTanque2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jLabelNivelEst_2)))
                    .addGroup(jPanelTanque2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanelTanque2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jLabelErro_2))))
                .addContainerGap())
        );

        jPanelTanque1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Tanque 1", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N
        jPanelTanque1.setFont(new java.awt.Font("Tahoma", 1, 12));

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel4.setText("Nivel Real:");

        jLabelNivelReal_1.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabelNivelReal_1.setText("0.00 cm ");

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel5.setText("Nível Estimado: ");

        jLabelNivelEst_1.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabelNivelEst_1.setText("0.00 cm");

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel6.setText("Erro: ");

        jLabelErro_1.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabelErro_1.setText("0.00 cm");

        javax.swing.GroupLayout jPanelTanque1Layout = new javax.swing.GroupLayout(jPanelTanque1);
        jPanelTanque1.setLayout(jPanelTanque1Layout);
        jPanelTanque1Layout.setHorizontalGroup(
            jPanelTanque1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelTanque1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelTanque1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanelTanque1Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabelNivelReal_1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanelTanque1Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabelNivelEst_1, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabelErro_1, javax.swing.GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanelTanque1Layout.setVerticalGroup(
            jPanelTanque1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelTanque1Layout.createSequentialGroup()
                .addGroup(jPanelTanque1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelTanque1Layout.createSequentialGroup()
                        .addGroup(jPanelTanque1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(jLabelNivelReal_1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
                        .addGroup(jPanelTanque1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(jLabelNivelEst_1)))
                    .addGroup(jPanelTanque1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanelTanque1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(jLabelErro_1))))
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanelTanquesLayout = new javax.swing.GroupLayout(jPanelTanques);
        jPanelTanques.setLayout(jPanelTanquesLayout);
        jPanelTanquesLayout.setHorizontalGroup(
            jPanelTanquesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelTanquesLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanelTanque1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(28, 28, 28)
                .addComponent(jPanelTanque2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanelTanquesLayout.setVerticalGroup(
            jPanelTanquesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelTanquesLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanelTanquesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanelTanque2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanelTanque1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        jMenuCONEXÃO.setText("Conexão");
        jMenuCONEXÃO.setFont(new java.awt.Font("Tahoma", 1, 12));

        jMenuCONFIGURAÇÔES.setFont(new java.awt.Font("Tahoma", 1, 12));
        jMenuCONFIGURAÇÔES.setText("Configurações");
        jMenuCONFIGURAÇÔES.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuCONFIGURAÇÔESActionPerformed(evt);
            }
        });
        jMenuCONEXÃO.add(jMenuCONFIGURAÇÔES);

        Conectar.setFont(new java.awt.Font("Tahoma", 1, 12));
        Conectar.setText("Conectar");
        Conectar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ConectarActionPerformed(evt);
            }
        });
        jMenuCONEXÃO.add(Conectar);

        ExecutarObs.setFont(new java.awt.Font("Tahoma", 1, 12));
        ExecutarObs.setText("Executar");
        ExecutarObs.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ExecutarObsActionPerformed(evt);
            }
        });
        jMenuCONEXÃO.add(ExecutarObs);

        Desconectar.setFont(new java.awt.Font("Tahoma", 1, 12));
        Desconectar.setText("Desconectar");
        Desconectar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DesconectarActionPerformed(evt);
            }
        });
        jMenuCONEXÃO.add(Desconectar);

        jMenuBar1.add(jMenuCONEXÃO);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelTanques, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(graficoPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 779, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(graficoPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 382, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelTanques, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void ConectarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ConectarActionPerformed
//    T_Planta = TipoPlanta.LINEAR;
    if (T_Planta != TipoPlanta.LINEAR) {
        if (!conectado) {
            if (!configurado) {
                JOptionPane.showMessageDialog(this, "Configurações ainda não foram realizadas", "Conectar", 1);
            } else {
                try {
                    cliente.conectar();
                    this.conectado = true;
                    JOptionPane.showMessageDialog(this, "Conexão realizada com sucesso", "Conectar", 1);
                } catch (QuanserClientException ex) {
                    JOptionPane.showMessageDialog(this, "Não foi possivel conectar ao servirdor", "Conectar", 1);
                }
            }
        } else {
            JOptionPane.showMessageDialog(this, "É necessario desconectar para realizar uma nova conexão", "Conectar", 1);
        }
    } else {
        conectado = true;
        JOptionPane.showMessageDialog(this, "Conexão realizada com sucesso", "Conectar", 1);
    }
}//GEN-LAST:event_ConectarActionPerformed

private void DesconectarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DesconectarActionPerformed
    // TODO add your handling code here:
    if (conectado) {
        limpaGrafico();
        contador.stop();
        conectado = false;
        if (T_Planta == TipoPlanta.SIMULADOR || T_Planta == TipoPlanta.REAL) {
            try {
                cliente.enviarSinal(0);
            } catch (QuanserClientException ex) {
                Logger.getLogger(Janela.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        try {
            exp.exportarDados();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Janela.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Janela.class.getName()).log(Level.SEVERE, null, ex);
        }
    } else {
        JOptionPane.showMessageDialog(this, "Uma conexão deve ser realizada antes de se desconectar", "Desconectar",1);
    }
}//GEN-LAST:event_DesconectarActionPerformed

    private void limpaGrafico() {
        gr = new Grafico("Gráfico");
        gr.pack();

        this.graficoPanel.removeAll();
        this.graficoPanel.add(gr.getContentPane());
        gr.setSize(graficoPanel.getSize().width + 8, graficoPanel.getSize().height + 31);
        tempoInicial = System.currentTimeMillis();
        tempoAtual = 0.0;
        tempoFinal = 0.0;
        //zera labels
        jLabelNivelReal_1.setText("0.00 cm");
        jLabelNivelEst_1.setText("0.00 cm");
        jLabelErro_1.setText("0.00 cm");
        jLabelNivelReal_2.setText("0.00 cm");
        jLabelNivelEst_2.setText("0.00 cm");
        jLabelErro_2.setText("0.00 cm");

    }

private void ExecutarObsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ExecutarObsActionPerformed

    if (!conectado) {
        JOptionPane.showMessageDialog(this, "É necessario realizar uma conexão antes da executar", "Executar", 1);
    } else {
        if (T_Planta != TipoPlanta.LINEAR) {
            executaPlantaNãoLinear();
        } else {
            executaPlantaLinear();
        }
    }
}//GEN-LAST:event_ExecutarObsActionPerformed

    private void jMenuCONFIGURAÇÔESActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuCONFIGURAÇÔESActionPerformed
        // TODO add your handling code here:
        JDialog.setDefaultLookAndFeelDecorated(true);
        jc.dispose();
        jc = new JanelaConfigurações(this, true, this);
        jc.setSize(350, 220);
        jc.setResizable(false);
        jc.setVisible(true);
    }//GEN-LAST:event_jMenuCONFIGURAÇÔESActionPerformed

    private void atualizarLabels(double nivelR1, double nivelR2, double erro1, double erro2, double nivelEst1, double nivelEst2) throws QuanserClientException {
        jLabelNivelReal_1.setText(aproximador.format(nivelR1) + " cm");
        jLabelNivelEst_1.setText(aproximador.format(nivelEst1) + " cm");
        jLabelErro_1.setText(aproximador.format(erro1) + " cm");
        jLabelNivelReal_2.setText(aproximador.format(nivelEst2) + " cm");
        jLabelNivelEst_2.setText(aproximador.format(nivelEst2) + " cm");
        jLabelErro_2.setText(aproximador.format(erro2) + " cm");
    }

    public boolean isConectado() {
        return conectado;
    }

    public void setConectado(boolean conectado) {
        this.conectado = conectado;
    }

    public boolean isConfigurado() {
        return configurado;
    }

    public void setConfigurado(boolean configurado) {
        this.configurado = configurado;
    }

    public void setClienteSimulador() {
        this.cliente = new Cliente("localhost", 20081);
    }

    public void setClientePlantaReal() {
        this.cliente = new Cliente("10.13.99.69", 20081);
    }

    public void setObsLinear() {
//        this.obs = new ObservadorLinear(-0.4938, -0.976);
        this.obs = new ObservadorLinear(.7624, .2614);
    }

    public Observador getObs() {
        return obs;
    }

    public void setObsOnline() {
        this.obs = new ObservadorCR();
    }

    public TipoObservador getT_Obs() {
        return T_Obs;
    }

    public void setT_Obs(TipoObservador T_Obs) {
        this.T_Obs = T_Obs;
    }

    public TipoPlanta getT_Planta() {
        return T_Planta;
    }

    public void setT_Planta(TipoPlanta T_Planta) {
        this.T_Planta = T_Planta;
    }

    public static void main(String args[]) {

        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;




                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Janela.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Janela.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Janela.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Janela.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>


        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Janela().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem Conectar;
    private javax.swing.JMenuItem Desconectar;
    private javax.swing.JMenuItem ExecutarObs;
    private javax.swing.JPanel graficoPanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabelErro_1;
    private javax.swing.JLabel jLabelErro_2;
    private javax.swing.JLabel jLabelNivelEst_1;
    private javax.swing.JLabel jLabelNivelEst_2;
    private javax.swing.JLabel jLabelNivelReal_1;
    private javax.swing.JLabel jLabelNivelReal_2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenu jMenuCONEXÃO;
    private javax.swing.JMenuItem jMenuCONFIGURAÇÔES;
    private javax.swing.JPanel jPanelTanque1;
    private javax.swing.JPanel jPanelTanque2;
    private javax.swing.JPanel jPanelTanques;
    // End of variables declaration//GEN-END:variables

    private void executaPlantaNãoLinear() {
        //double a = System.currentTimeMillis();

        ActionListener observador = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    gr.adicionarValores((tempoAtual / 1000), cliente.getNivelTanque1(), cliente.getNivelTanque2(), obs.getXck1(), obs.getXck2(), obs.getSinalEscrito(), obs.getErro1(), obs.getErro2());
                    tempoAtual = (tempoFinal + ((Double) (System.currentTimeMillis() - tempoInicial)));
                    obs.run(cliente.getNivelTanque1(), cliente.getNivelTanque2());
                    cliente.enviarSinal(obs.getSinalEscrito());
                    atualizarLabels(cliente.getNivelTanque1(), cliente.getNivelTanque2(), obs.getErro1(), obs.getErro2(), obs.getXck1(), obs.getXck2());
                    exp.AddErro(obs.getErro1(), obs.getErro2());
                    exp.AddNivelEst(obs.getXck1(), obs.getXck2());
                    exp.AddNivelReal(cliente.getNivelTanque1(), cliente.getNivelTanque2());
                } catch (QuanserClientException ex) {
                    Logger.getLogger(Janela.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        };
//        System.out.println("                    TEMPO:" + (System.currentTimeMillis() - a));
        tempoInicial = System.currentTimeMillis();
        contador = new Timer(periodoAmst, observador);
        contador.start();
    }

    private void executaPlantaLinear() {
        ActionListener observador = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    gr.adicionarValores((tempoAtual / 1000), obs.getPlantaLinear_1(), obs.getPlantaLinear_2(), obs.getXck1(), obs.getXck2(), obs.getSinalEscrito(), obs.getErro1(), obs.getErro2());
                    tempoAtual = (tempoFinal + ((Double) (System.currentTimeMillis() - tempoInicial)));
                    obs.plantaLinaer(obs.getSinalEscrito());
                    obs.run(obs.getPlantaLinear_1(), obs.getPlantaLinear_2());
                    atualizarLabels(obs.getPlantaLinear_1(), obs.getPlantaLinear_2(), obs.getErro1(), obs.getErro2(), obs.getXck1(), obs.getXck2());
                    exp.AddErro(obs.getErro1(), obs.getErro2());
                    exp.AddNivelEst(obs.getXck1(), obs.getXck2());
                    exp.AddNivelReal(obs.getPlantaLinear_1(), obs.getPlantaLinear_2());
                } catch (QuanserClientException ex) {
                    Logger.getLogger(Janela.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        };

        tempoInicial = System.currentTimeMillis();
        contador = new Timer(periodoAmst, observador);
        contador.start();
    }
}
